package com.syyo.gulimall.member.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.syyo.common.enums.ResultEnum;
import com.syyo.common.exception.SysException;
import com.syyo.gulimall.member.domain.entity.UndoLogEntity;
import com.syyo.gulimall.member.domain.req.UndoLogReq;
import com.syyo.gulimall.member.mapper.UndoLogMapper;
import com.syyo.gulimall.member.service.UndoLogService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


/**
 * @author syyo
 * @email syyo@gmail.com
 * @date 2021-01-20 09:52:10
 */
@Service
public class UndoLogServiceImpl extends ServiceImpl<UndoLogMapper, UndoLogEntity> implements UndoLogService {

    @Autowired
    private UndoLogMapper undoLogMapper;


    /**
     * 新增
     */
    @Override
    @Transactional
    public int add(UndoLogReq req) {
        UndoLogEntity entity = new UndoLogEntity();
        // 添加创建时间和更新时间
        BeanUtils.copyProperties(req, entity);
        int insert = undoLogMapper.insert(entity);
        if (insert != 1) {
            throw new SysException(ResultEnum.E_10001.getCode(), ResultEnum.E_10001.getMessage());
        }
        return insert;
    }

    /**
     * 删除
     */
    @Override
    @Transactional
    public int del(Integer id) {
        int insert = undoLogMapper.deleteById(id);
        if (insert != 1) {
            throw new SysException(ResultEnum.E_10002.getCode(), ResultEnum.E_10002.getMessage());
        }
        return insert;
    }

    /**
     * 编辑
     */
    @Override
    @Transactional
    public int edit(UndoLogReq req) {
        UndoLogEntity entity = new UndoLogEntity();
        // 添加更新时间
        BeanUtils.copyProperties(req, entity);
        int insert = undoLogMapper.updateById(entity);
        if (insert != 1) {
            throw new SysException(ResultEnum.E_10003.getCode(), ResultEnum.E_10003.getMessage());
        }
        return insert;
    }

    /**
     * 详情
     */
    @Override
    public UndoLogEntity findOne(Integer id) {
        return undoLogMapper.selectById(id);
    }

    /**
     * 列表
     */
    @Override
    public IPage<UndoLogEntity> findAll(Integer pageNum, Integer pageSize, UndoLogReq req) {
        Page<UndoLogEntity> teacherPage = new Page<UndoLogEntity>(pageNum, pageSize);
        QueryWrapper<UndoLogEntity> wrapper = new QueryWrapper<>();
        return undoLogMapper.selectPage(teacherPage, wrapper);
    }

}